<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *订单管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$manage_op=array('confirm','agree','disagree');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择订单！','error');
	}
}
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'订单总列表'),
);
$return['status']=$this->fuwuorder_status;
$return['order_types']=$this->order_types;
if($_RQ['op']=='list'){
	pdo_update('jy_shoufrom_order',array('ispay'=>1),array('uniacid'=>$_SESSION['uniacid'],'ispay'=>0,'pay_id >'=>0,'status'=>array(1,2,4,5,6,7)));
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_shoufrom_order',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		$get_pay_types=get_pay_types();
		foreach($list as &$item){
			$item['serve_title']=pdo_getcolumn('jy_shoufrom_list',array('id'=>$item['pid'],'uniacid'=>$_SESSION['uniacid']),'title');
			$item['paytype']=pdo_getcolumn('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['pay_id']),'pay_type');
			if($item['isyuejie'] && !$item['paytype']){
				$item['paytype']='yuejie';
			}
			$item['paytypetitle']=$get_pay_types[$item['paytype']];
			$item['worker_name']=pdo_getcolumn('jy_wxshifu_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['shifuid']),'title');
			$item['express']=pdo_get('jy_shoufrom_order_express',array('orderid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
			if($item['express']['id']){
				$item['isexpress']=1;
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('type'=>'string_more','title'=>'客户信息','data'=>array(
				array('title'=>'姓名：','name'=>'username'),
				array('title'=>'电话：','name'=>'mobile'),
				)
			),
			//array('name'=>'worker_name','title'=>'师傅/技术员','type'=>'string'),
			array('name'=>'serve_title','title'=>'项目名称','type'=>'string'),
			array('name'=>'type','title'=>'类型','type'=>'radio','options'=>'order_types'),
			//array('name'=>'serve_time','title'=>'服务时间','type'=>'time'),
			array('type'=>'string_more','title'=>'金额','data'=>array(
				array('title'=>'金额：','name'=>'price'),
				array('title'=>'支付：','name'=>'payfee')
				)
			),
			array('name'=>'paytypetitle','title'=>'支付方式','type'=>'string'),
			array('name'=>'ispay','title'=>'支付状态','type'=>'radio','options'=>'ispay'),
			array('name'=>'remark','title'=>'备注','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'href','op'=>'look','title'=>'订单详情'),
			array('type'=>'modal','op'=>'express','title'=>'快递信息','show'=>array('isexpress'=>1)),
			array('type'=>'post','op'=>'confirm','message'=>'确定要接单吗？','title'=>'接单','show'=>array('status'=>1)),
			array('type'=>'post','op'=>'shouhuo','message'=>'确定已收到货吗？','title'=>'确认收货','show'=>array('status'=>3)),
			array('type'=>'post','op'=>'status5','message'=>'确定开始处理维修吗？','title'=>'开始处理','show'=>array('status'=>4)),
			array('type'=>'modal','op'=>'status6','title'=>'提交处理结果','show'=>array('status'=>5)),
			array('type'=>'modal','op'=>'fa','title'=>'发货寄回产品','show'=>array('status'=>6)),
			array('type'=>'modal','op'=>'cancel','title'=>'取消','message'=>'取消订单？','show'=>array('status'=>1)),
		)),
		'search'=>array(
			array('name'=>'username','width'=>15,'title'=>'用户姓名','type'=>'string'),
			array('name'=>'worker_name','width'=>15,'title'=>'员工姓名','type'=>'string'),
		)
	);
	$return['ispay']=array('未支付','已支付');
	$return['htmls']=$htmls;
	exi($return);
}
//确认订单操作
if($_RQ['op']=='confirm'){
	if($_RQ['id']){
		pdo_update('jy_shoufrom_order',array('status'=>'2'),array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
	}
	exi('处理成功');
}
if($_RQ['op']=='shouhuo'){
	if($_RQ['id']){
		pdo_update('jy_shoufrom_order',array('status'=>4),array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功');
}
if($_RQ['op']=='status6'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('参数错误！','error');
		}
		if($post['chulitype'] && !$post['shifuremark']){
			exi('请填写处理结果','error');
		}
		pdo_update('jy_shoufrom_order',array('status'=>6,'shifuremark'=>$post['shifuremark'],'chulitype'=>$post['chulitype']),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));			
		exi('订单处理成功！','','/fuwu.order/list/');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请选择订单！','error');
	}
	$htmls=array(
		'type'=>'edit',
		'desc'=>'处理结果',
	);
	$item=pdo_get('jy_shoufrom_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$tip='提交最后的处理结果；';
	$htmls['edit']=array(
		array('type'=>'tip','title'=>$tip),
		array('type'=>'radio','title'=>'结果类型','name'=>'chulitype','options'=>'chulitype'),
		array('type'=>'text','title'=>'处理结果描述','name'=>'shifuremark'),
	);
	$return['chulitype']=$this->chulitype;
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='status5'){
	if($_RQ['id']){
		pdo_update('jy_shoufrom_order',array('status'=>5),array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功');
}
if($_RQ['op']=='fa'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('参数错误！','error');
		}
		if(!$post['express_id2'] || !$post['expresssn2']){
			exi('请填写快递信息','error');
		}
		$data=array(
			'express_id2'=>intval($post['express_id2']),
			'expresssn2'=>$post['expresssn2'],
		);
		pdo_update('jy_shoufrom_order_express',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		pdo_update('jy_shoufrom_order',array('status'=>7),array('id'=>$post['orderid'],'uniacid'=>$_SESSION['uniacid']));			
		exi('订单处理成功！','','/fuwu.order/list/');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请选择订单！','error');
	}
	$htmls=array(
		'type'=>'edit',
		'desc'=>'寄回产品',
	);
	$order=pdo_get('jy_shoufrom_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$item=pdo_get('jy_shoufrom_order_express',array('orderid'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$item['username']=$order['username'];
	$item['mobile']=$order['mobile'];
	$tip='后台处理寄回产品；';
	$htmls['edit']=array(
			array('type'=>'tip','title'=>$tip),
			array('type'=>'copy','title'=>'联系人','name'=>'username'),
			array('type'=>'copy','title'=>'联系电话','name'=>'mobile'),
			array('type'=>'copy','title'=>'寄回地址','name'=>'address'),
			array('type'=>'select','title'=>'快递公司','name'=>'express_id2','options'=>'express','option_title'=>'name'),
			array('type'=>'string','title'=>'快递单号','name'=>'expresssn2'),
	);
	$return['express']=model('jy_weishop/model')->express_types();
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='cancel'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('参数错误！','error');
		}
		if(!$post['shifuremark']){
			exi('请填写取消原因','error');
		}
		pdo_update('jy_shoufrom_order',array('status'=>9,'shifuremark'=>$post['shifuremark'],'chulitype'=>2),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));			
		exi('取消订单成功！','','/fuwu.order/list/');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请选择订单！','error');
	}
	$htmls=array(
		'type'=>'edit',
		'desc'=>'取消操作',
	);
	$item=pdo_get('jy_shoufrom_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$tip='提交最后的取消原因；';
	$htmls['edit']=array(
		array('type'=>'tip','title'=>$tip),
		array('type'=>'text','title'=>'取消原因','name'=>'shifuremark'),
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
//同意退款
if($_RQ['op']=='agree'){
	if($_RQ['id']){
		pdo_update('jy_shoufrom_order',array('status'=>'6'),array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
	}
	exi('已同意退款申请！');
}
//拒绝退款操作
if($_RQ['op']=='disagree'){
	if($_RQ['id']){
		pdo_update('jy_shoufrom_order',array('status'=>'7'),array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
	}
	exi('已拒绝退款申请！');
}
if($_RQ['op']=='express'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_shoufrom_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		exi('请选择订单','error');
	}
	$item=pdo_get('jy_shoufrom_order_express',array('orderid'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$express_types=model('jy_weishop/model')->express_types();
	$item['express_name']=$express_types[$item['express_id']]['name'];
	$item['express_name2']=$express_types[$item['express_id2']]['name'];
	$return['item']=$item;
	$htmls=array(
		'type'=>'look',
		'desc'=>'订单信息',
		'look'=>array(
				array('title'=>'客户寄产品','type'=>'help','help'=>''),
				array('name'=>'express_name','title'=>'客户寄产品快递','type'=>'string'),
				array('name'=>'expresssn','title'=>'客户寄产品单号','type'=>'string'),
				array('title'=>'平台回寄','type'=>'help','help'=>''),
				array('name'=>'express_name2','title'=>'平台回寄快递','type'=>'string'),
				array('name'=>'expresssn2','title'=>'平台回寄单号','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='look'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_shoufrom_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		exi('请选择订单','error');
	}
	$item['images']=json_decode($item['images'],true);
	$item['serve_title']=pdo_getcolumn('jy_shoufrom_list',array('id'=>$item['pid'],'uniacid'=>$_SESSION['uniacid']),'title');
	$item['paytype']=pdo_getcolumn('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'oid'=>$item['id'],'plugin'=>'jy_shoufrom'),'pay_type');
	$item['paytype']=get_pay_types()[$item['paytype']];
	$item['total']=$item['total']?:1;
	$item['diyfields']=$item['diyfields']?json_decode($item['diyfields'],true):array();
	$formtypes=get_form_types();	
	foreach($item['diyfields'] as &$v){
		if($v['type']==5 && $v['data']){
			$v['data'] = explode(";", $v['data']);
			foreach($v['data'] as $k=>&$i){
				if(empty($v['data'][$k])){
					unset($v['data'][$k]);
				}
			}
		}
		//$v['type']=$formtypes[$v['type']];
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'look',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回订单列表'),
		),
		'desc'=>'订单信息',
		'look'=>array(
				array('name'=>'username','title'=>'用户姓名','type'=>'string'),
				array('name'=>'mobile','title'=>'用户电话','type'=>'string'),
				array('name'=>'serve_title','title'=>'项目名称','type'=>'string'),
				array('name'=>'price','title'=>'金额','type'=>'string'),
				array('name'=>'remark','title'=>'故障描述','type'=>'text'),
				array('name'=>'images','title'=>'故障拍照','type'=>'images'),
				array('name'=>'video','title'=>'故障视频','type'=>'video'),
				array(
						'title'=>'表单详情',
						'type'=>'array',
						'name'=>'diyfields',
						'list'=>array(
							array('type'=>'string','name'=>'title','title'=>'表单标题'),	
							array('type'=>'labelarray','name'=>'data','title'=>'表单内容'),	
						),
				),
				array('type'=>'radio','title'=>'处理结果类型','name'=>'chulitype','options'=>'chulitype'),
				array('type'=>'text','title'=>'处理结果描述','name'=>'shifuremark'),
		)
	);
	$return['chulitype']=$this->chulitype;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export'){
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$params['uniacid']=$_SESSION['uniacid'];
	$order=pdo_getall('jy_shoufrom_order',$params);
	if($order){
		foreach($order as &$item){
			$item['serve_title']=pdo_getcolumn('jy_shoufrom_list',array('id'=>$item['pid'],'uniacid'=>$_SESSION['uniacid']),'title');
			$item['paytype']=pdo_getcolumn('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'oid'=>$item['id'],'plugin'=>'jy_shoufrom'),'pay_type');
			$item['paytype']=get_pay_types()[$item['paytype']];
			$item['serve_time']=date('Y-m-d H:i:s',$item['serve_time']);
			$item['typetitle']=$this->order_types[$item['type']];
			$item['status']=$this->fuwuorder_status[$item['status']];
		}
	}
	$header=array(
		'ordersn'=>'订单号',
		'username'=>'用户姓名',
		'mobile'=>'用户电话',
		'serve_title'=>'项目名称',
		'status'=>'类型',
		'serve_time'=>'服务时间',
		'price'=>'金额',
		'payfee'=>'实际支付',
		'paytype'=>'支付方式',
		'remark'=>'备注',
		'status'=>'状态',
	);
	cfc('tools')->export2excel($header,$order,'订单');
}
